package com.abocide.appium.sdk.core.xml;


import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class Node {
    private Integer index;
    private String className;
    private String resourceId;
    private String packageName;
    private Location location;
    private Integer depth;
    private Integer instance;
    private String contentDesc;
    private String key;
    private String parentKey;
    private Map<String, String> attributes;
    private List<Node> nodes;

    public static class Location {
        private BigDecimal bottom;
        private BigDecimal left;
        private BigDecimal right;
        private BigDecimal top;

        public BigDecimal getBottom() {
            return bottom;
        }

        public void setBottom(BigDecimal bottom) {
            this.bottom = bottom;
        }

        public BigDecimal getLeft() {
            return left;
        }

        public void setLeft(BigDecimal left) {
            this.left = left;
        }

        public BigDecimal getRight() {
            return right;
        }

        public void setRight(BigDecimal right) {
            this.right = right;
        }

        public BigDecimal getTop() {
            return top;
        }

        public void setTop(BigDecimal top) {
            this.top = top;
        }

        public BigDecimal getX() {
            return this.getLeft();
        }

        public BigDecimal getY() {
            return this.getTop();
        }

        public BigDecimal getWidth() {
            return this.getRight().subtract(this.getLeft());
        }

        public BigDecimal getHeight() {
            return this.getBottom().subtract(this.getTop());
        }

    }

    public String getAttribute(String key) {
        if (this.attributes == null) {
            return null;
        }
        return this.attributes.get(key);
    }


    public Integer getIndex() {
        return index;
    }

    public void setIndex(Integer index) {
        this.index = index;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public String getResourceId() {
        return resourceId;
    }

    public void setResourceId(String resourceId) {
        this.resourceId = resourceId;
    }

    public String getPackageName() {
        return packageName;
    }

    public void setPackageName(String packageName) {
        this.packageName = packageName;
    }

    public Location getLocation() {
        return location;
    }

    public void setLocation(Location location) {
        this.location = location;
    }

    public Integer getDepth() {
        return depth;
    }

    public void setDepth(Integer depth) {
        this.depth = depth;
    }

    public Integer getInstance() {
        return instance;
    }

    public void setInstance(Integer instance) {
        this.instance = instance;
    }

    public Map<String, String> getAttributes() {
        return attributes;
    }

    public void setAttributes(Map<String, String> attributes) {
        this.attributes = attributes;
    }

    public List<Node> getNodes() {
        return nodes;
    }

    public void setNodes(List<Node> nodes) {
        this.nodes = nodes;
    }

    public String getContentDesc() {
        return contentDesc;
    }

    public void setContentDesc(String contentDesc) {
        this.contentDesc = contentDesc;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getParentKey() {
        return parentKey;
    }

    public void setParentKey(String parentKey) {
        this.parentKey = parentKey;
    }

    /**
     * 返回子元素数量大于1的集合
     *
     * @return
     */
    public List<Node> getDeepNodes() {
        boolean hasNext = true;
        Node curNode = this;
        do {
            List<Node> nodes = curNode.getNodes();
            if (CollectionUtils.isEmpty(nodes)) {
                return new ArrayList<>();
            } else if (nodes.size() == 1) {
                curNode = nodes.get(0);
            }
            hasNext = nodes.size() == 1;
        } while (hasNext);
        return curNode.getNodes();
    }
}
